#!/usr/bin/env python3
"""
Main entry point for the PDF Splitter application.
"""

import sys
import logging
from PyQt6.QtWidgets import QApplication
from PyQt6.QtGui import QIcon
from .gui.main_window import MainWindow
from .i18n import Translator
from .utils.settings_manager import get_settings_manager


def setup_logging():
    """Setup logging configuration."""
    # Create logs directory
    import os
    from pathlib import Path

    log_dir = Path.home() / ".pdf_splitter" / "logs"
    log_dir.mkdir(parents=True, exist_ok=True)

    # Configure logging
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        handlers=[
            logging.FileHandler(log_dir / "pdf_splitter.log"),
            logging.StreamHandler(sys.stdout)
        ]
    )


def main() -> int:
    """Main application entry point."""
    # Setup logging
    setup_logging()
    logger = logging.getLogger(__name__)
    logger.info("Starting PDF Splitter application...")

    # Create QApplication instance
    app = QApplication(sys.argv)
    app.setApplicationName("PDF Splitter")
    app.setApplicationVersion("0.1.0")
    app.setOrganizationName("PDF Splitter")
    app.setOrganizationDomain("pdfsplitter.com")

    # Set application icon
    app.setWindowIcon(QIcon(":/icons/app_icon.png"))

    # Initialize translation system
    translator = Translator()

    # Set global translator BEFORE creating main window
    from .i18n import set_global_translator
    set_global_translator(translator)

    # Load saved language preference if exists
    settings_manager = get_settings_manager()
    settings = settings_manager.get_settings()
    if hasattr(settings, 'language') and settings.language:
        translator.load_language(settings.language)

    # Create and show main window
    main_window = MainWindow(translator)
    main_window.show()

    logger.info("Application window created and shown")

    # Run event loop
    return app.exec()


if __name__ == "__main__":
    sys.exit(main())